package TenoriOn.managers
{
	import flash.geom.Point;
	import TenoriOn.Grid;
	import TenoriOn.GridData;

	public class VolumeManager implements IGridManager
	{
		private var _grid:Grid;
		
		private var _hasControl:Boolean;
		public function get hasControl():Boolean{
			return _hasControl;
		}
		
		public function get MANAGER_TYPE():String{
			return "VolumeManager";
		}
		
		public function VolumeManager()
		{
		}
		
		public function makeGridData():GridData
		{
			var level:int=Math.round(_volume*15);
			var gData:GridData=new GridData();
			for(var i:int=0;i<16;i++){
				gData.data[i][level]=1;
			}
			return gData;
		}
		
		public function takeControl(grid:Grid):void{
			grid.manager=this;
			_hasControl=true;
			_grid=grid;
			updateGrid();
		}
		
		public function releaseControl():void
		{
			_hasControl=false;
		}
		
		public function onGridUpdate(id:Point):void{
			volume=Math.round(100*id.y/15)/100;
			updateGrid();
		}
		
		public function updateGrid():void{
			_grid.redrawGrid(makeGridData());
			_grid.textDisplay.setText("Volume Manager","VOLUME:"+Math.round(15*volume));
		}
		
		private var _volume:Number=0.7;
		public function get volume():Number{
			return _volume;
		}
		public function set volume(value:Number):void{
			if(value<=1&&value>=0)
				_volume=value;
			else
				throw new Error("Invalid value "+value.toString()+" for TenoriOn.VolumeManager.volume -> valid range=0-1");
		}
	}
}